package com.hzit.consumer.controller;

import com.hzit.api.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

//@RestController
//@RequestMapping("/consumer")
public class ConsumerController {

    //xxxService

    private static final String PREFIX_URL = "http://SPRINGCLOUD-DEPT/";
    //private static final String PREFIX_URL = "http://localhost:8001";
    /**
     * Rest提供的模板类，可以调用指定的路径
     */
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = {"/{deptno}"})
    public Dept get(@PathVariable int deptno) {

        //HttpClient
        //Rest
        //restTemplate:提供很多方法,http请求类似
        //http://localhost:8001/dept/get/1

        String url = PREFIX_URL + "dept/get/" + deptno;

        //只能得到对象（响应体）
        Dept dept = restTemplate.getForObject(url, Dept.class);

        ResponseEntity<Dept> entity = restTemplate.getForEntity(url, Dept.class);
        entity.getStatusCodeValue(); //响应状态码
        entity.getHeaders(); //响应头
        Dept body = entity.getBody(); //响应体，getForObject()功能一致


        return dept;
    }


    @PostMapping("/add")
    public boolean add(@RequestBody Dept dept) {

        String url = PREFIX_URL + "dept/add";
        Boolean post = restTemplate.postForObject(url, dept, Boolean.class);
        return post;
    }

}
